home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / PrintManager.C < prev    next >
C/C++ Source or Header  |  1992-08-10  |  11KB  |  470 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "PrintManager.h"
  6.  
  7. #include "Class.h"
  8. #include "OrdColl.h"
  9. #include "PrintPort.h"
  10. #include "FileDialog.h"
  11. #include "CollView.h"
  12. #include "Fields.h"
  13. #include "BorderItems.h"
  14. #include "Buttons.h"
  15. #include "Printer.h"
  16. #include "ClassManager.h"
  17. #include "Box.h"
  18. #include "String.h"
  19. #include "Expander.h"
  20. #include "Scroller.h"
  21. #include "Env.h"
  22.  
  23. const int cNameMinWidth= 250;
  24.  
  25. bool gPrinting;
  26. SmartPrintManager gPrintManager;
  27.  
  28. PrintManager *SmartPrintManager::MakePrintManager()
  29. {
  30.     if (pm == 0)
  31.     pm= new PrintManager;
  32.     return pm;
  33. }
  34.  
  35. //---- PrintDialog -------------------------------------------------------------
  36.  
  37. class PrintDialog : public Dialog {
  38. public:
  39.     FileDialog *saveDialog;
  40.     CompositeVObject *scroller;
  41.     CollectionView *collview;
  42.     ToggleButton *options;
  43.     IntField *from, *to;
  44.     char *title;
  45.     TextItem *no;
  46.     Box *clu;
  47.     OrdCollection *printernames;
  48.        
  49. public:
  50.     MetaDef(PrintDialog);
  51.     
  52.     PrintDialog(char *title= "Print");
  53.     ~PrintDialog();    
  54.     VObject *DoMakeContent();
  55.     void Control(int id, int, void *v);
  56.     void UpdateButtons();
  57.     
  58.     void StoreSettings(PrinterSettings*);
  59.     void DoSetDefaults();
  60.     void DoSetup();
  61. };
  62.  
  63. //---- PrinterSettings --------------------------------------------------------
  64.  
  65. NewMetaImpl(PrinterSettings, Object, (T(paperSize), T(topLeftMargin), T(bottomRightMargin),
  66.                             TS(name), TB(showgrid)));
  67.  
  68. PrinterSettings::PrinterSettings(Point ps, Point tlm, Point brm, Rectangle)
  69. {
  70.     paperSize= ps;
  71.     topLeftMargin= tlm;
  72.     bottomRightMargin= brm;
  73.     showgrid= FALSE;
  74. }
  75.  
  76. PrinterSettings::~PrinterSettings()
  77. {
  78. }
  79.  
  80. Printer *PrinterSettings::GetPrinter()
  81. {
  82.     return gPrintManager->FindPrinter(name);
  83. }
  84.  
  85. Rectangle PrinterSettings::GetPrintRect()
  86. {
  87.     return Rectangle(paperSize).Inset(10);
  88. }
  89.  
  90. Rectangle PrinterSettings::GetViewRect()
  91. {
  92.     return Rectangle(topLeftMargin, paperSize - (topLeftMargin+bottomRightMargin));
  93. }
  94.  
  95. void PrinterSettings::SetPaperSize(Point e)
  96. {
  97.     paperSize= e;
  98. }
  99.  
  100. void PrinterSettings::SetTopLeftMargin(Point tlm)
  101. {
  102.     topLeftMargin= tlm;
  103. }
  104.  
  105. void PrinterSettings::SetBottomRightMargin(Point brm)
  106. {
  107.     bottomRightMargin= brm;
  108. }
  109.  
  110. void PrinterSettings::ShowPageBreaks(bool mode)
  111. {
  112.     showgrid= mode;
  113. }
  114.  
  115. OStream& PrinterSettings::PrintOn (OStream &s)
  116. {
  117.     Object::PrintOn(s);
  118.     return s << paperSize SP << topLeftMargin SP << bottomRightMargin SP
  119.                             << name SP << showgrid SP;
  120. }
  121.  
  122. IStream& PrinterSettings::ReadFrom(IStream &s)
  123. {
  124.     char buf[100];
  125.     Object::ReadFrom(s);
  126.     s >> paperSize >> topLeftMargin >> bottomRightMargin >> buf >> showgrid;
  127.     name= strsave(buf);
  128.     return s;
  129. }
  130.  
  131. //---- PrintManager ------------------------------------------------------------
  132.  
  133. NewMetaImpl(PrintManager, Object, (TP(currSettings), TP(printers)));
  134.  
  135. PrintManager::PrintManager()
  136. {
  137.     printers= new OrdCollection;
  138.     
  139.     Iter next(gClassManager->Iterator());
  140.     Class *cla;
  141.     
  142.     while (cla= (Class*)next())
  143.     if (cla->isKindOf(Meta(Printer)))
  144.         printers->Add((Printer*) cla->Proto()->New());
  145.         
  146.     InstallPrinter(Env::GetValue("Printer.Default", "GENERIC"));
  147. }
  148.  
  149. void PrintManager::InstallPrinter(char *name)
  150. {
  151.     Printer *pr= FindPrinter(name);
  152.     if (pr) {
  153.     currPrinter= pr;
  154.     currSettings= currPrinter->GetSettings();
  155.     Send(cIdNone, cPartAnyChange);
  156.     }
  157. }
  158.  
  159. Printer *PrintManager::FindPrinter(char *name)
  160. {
  161.     Iter next(printers);
  162.     Printer *printer;
  163.     
  164.     while (printer= (Printer*)next())
  165.     if (strcmp(name, printer->GetName()) == 0)
  166.         return printer;
  167.     return 0;
  168. }
  169.  
  170. int PrintManager::PrinterId(Printer *pr)
  171. {
  172.     Iter next(printers);
  173.     Printer *printer;
  174.     
  175.     for (int i= 0; printer= (Printer*)next(); i++)
  176.     if (printer == pr)
  177.         return i;
  178.     return 0;
  179. }
  180.  
  181. PrintManager::~PrintManager()
  182. {
  183.     if (printers) {
  184.     printers->FreeAll();
  185.     SafeDelete(printers);
  186.     }
  187. }
  188.  
  189. bool PrintManager::GetShowPageBreaks(VObject *v)
  190.     return currSettings->GetShowPageBreaks() || (v && v->TestFlag(eVObjShowPage)); 
  191. }
  192.  
  193. void PrintManager::ShowPageGrid(Rectangle r, VObject *v)
  194. {
  195.     if (!gPrinting && GetShowPageBreaks(v) && GetPrinter()) {
  196.     int i;
  197.     Point pgs= GetViewRect().extent;
  198.     Rectangle pgr;
  199.     Point e= v->GetExtent();
  200.     View *view;
  201.  
  202.     if (v->IsKindOf(View))
  203.         view= (View*) v;
  204.     else
  205.         view= 0;
  206.  
  207.     GrSetPenSize(1);
  208.     GrSetPenInk(ePatGrey50);
  209.     //for (x= ps.x; x <= e.x; x+= ps.x)
  210.     //    GrLine(Point(x,0), Point(x,e.y));
  211.     int np= 1;
  212.     for (i= pgr.origin.y= 0; pgr.origin.y <= r.origin.y+r.extent.y; pgr.origin.y+= pgr.extent.y, i++) {
  213.         pgr.extent= pgs;
  214.         if (view)
  215.         pgr= view->NextPageBreak(np, pgr);
  216.         if (i > 0)
  217.         GrLine(Point(0, pgr.origin.y), Point(e.x, pgr.origin.y));
  218.         np++;
  219.     }
  220.     GrSetPenNormal();
  221.    }
  222. }
  223.  
  224. void PrintManager::Print(char *name, int from, int to)
  225. {
  226.     bool done= FALSE;
  227.     View *view;
  228.     Printer *printer= GetPrinter();
  229.  
  230.     Point pgs= GetViewRect().extent;
  231.     Rectangle pgr;
  232.     Rectangle vr= vobject->ContentRect();
  233.     
  234.     int np= 1, pgcnt= 0;
  235.     
  236.     if (vobject->IsKindOf(View))
  237.     view= (View*) vobject;
  238.     else
  239.     view= 0;
  240.  
  241.     np= 1;
  242.     pgcnt= 0;
  243.     for (pgr.origin.y= 0; pgr.origin.y < vr.extent.y; pgr.origin.y+= pgr.extent.y) {
  244.     for (pgr.origin.x= 0; pgr.origin.x < vr.extent.x; pgr.origin.x+= pgr.extent.x) {
  245.         pgr.extent= pgs;
  246.         if (view)
  247.         pgr= view->NextPageBreak(np, pgr);
  248.         if (np >= from && np <= to)
  249.         pgcnt++;
  250.         np++;
  251.     }
  252.     }
  253.     printer->Start(pgcnt);
  254.     PrintPort *printport= printer->MakePrintPort(name);
  255.     np= 1;
  256.     for (pgr.origin.y= 0; !done && pgr.origin.y < vr.extent.y; pgr.origin.y+= pgr.extent.y) {
  257.     for (pgr.origin.x= 0; pgr.origin.x < vr.extent.x; pgr.origin.x+= pgr.extent.x) {
  258.         pgr.extent= pgs;
  259.         if (view)
  260.         pgr= view->NextPageBreak(np, pgr);
  261.         
  262.         if (np >= from && np <= to) {
  263.         if (printer->OpenPage(np, pgs)) {
  264.             done= TRUE;
  265.             break;
  266.         }
  267.  
  268.         PrintPage(np, printport, pgr);
  269.  
  270.         if (printer->ClosePage()) {
  271.             done= TRUE;
  272.             break;
  273.         }
  274.         }
  275.         np++;
  276.     }
  277.     }
  278.     SafeDelete(printport);
  279.     printer->Finish();
  280. }
  281.  
  282. void PrintManager::PrintPage(int np, PrintPort *printport, Rectangle pgr)
  283. {
  284.     View *view= 0;
  285.     if (vobject->IsKindOf(View))
  286.     view= (View*)vobject;
  287.     
  288.     printport->OpenPage(np);
  289.  
  290.     Port *oldport= GrGetPort();
  291.     GrSetPort(printport);
  292.  
  293.     if (view) {
  294.     gPrinting= TRUE;
  295.     view->PrintAdorn(GetPaperSize(), np);
  296.     }  
  297.  
  298.     printport->ClipFurther(GetViewRect());
  299.     printport->Translate(GetTopLeftMargin()-pgr.origin);
  300.  
  301.     vobject->DrawAll(pgr, FALSE);
  302.     
  303.     if (view)
  304.     gPrinting= FALSE;
  305.     GrSetPort(oldport);
  306.  
  307.     printport->ClosePage();
  308. }
  309.  
  310. void PrintManager::ShowPrintDialog(VObject *v)
  311. {
  312.     if (v) {
  313.     vobject= v;
  314.     PrintDialog *pd= MakePrintDialog();
  315.     if (pd) {
  316.         pd->ShowOnWindow(vobject->GetWindow());
  317.         delete pd;
  318.     }
  319.     }
  320. }
  321.  
  322. PrintDialog *PrintManager::MakePrintDialog()
  323. {
  324.     return new PrintDialog("Print Dialog");
  325. }
  326.  
  327. //---- PrintDialog -------------------------------------------------------------
  328.  
  329. const int cIdSaveToFile =   cIdFirstUser + 2,
  330.       cIdFrom       =   cIdFirstUser + 3,
  331.       cIdTo         =   cIdFirstUser + 4,
  332.       cIdPrint      =   cIdFirstUser + 5,
  333.       cIdPrinters   =   cIdFirstUser + 100,
  334.       cIdOptions    =   cIdFirstUser + 200;
  335.  
  336. NewMetaImpl(PrintDialog, Dialog, (TP(saveDialog), TP(collview), TP(from), TP(to), TP(title)));
  337.  
  338. PrintDialog::PrintDialog(char *t) : Dialog(t)
  339. {
  340.     title= t;
  341. }
  342.  
  343. PrintDialog::~PrintDialog()
  344. {
  345.     SafeDelete(collview);
  346. }
  347.  
  348. void PrintDialog::DoSetDefaults()
  349. {
  350.     from->SetValue(1);
  351.     to->SetValue(999);
  352.     int id= gPrintManager->PrinterId(gPrintManager->GetPrinter());
  353.     collview->SetSelection(Rectangle(0, id, 1, 1));    
  354.     options->SetValue(gPrintManager->currSettings->showgrid ? 1 : 0);
  355. }
  356.  
  357. void PrintDialog::DoSetup()
  358. {
  359.     EnableItem(cIdSaveToFile, gPrintManager->GetPrinter()->CanSave());
  360.     EnableItem(cIdPrint, gPrintManager->GetPrinter()->CanPrint());
  361. }
  362.  
  363. void PrintDialog::StoreSettings(PrinterSettings *settings)
  364. {
  365.     Printer *p= gPrintManager->GetPrinter();
  366.     settings->showgrid= options->GetValue();
  367.     if (p && p->GetOptions())
  368.     p->StoreSettings(settings);
  369.     gPrintManager->Send(cIdNone, cPartAnyChange);
  370. }
  371.  
  372. VObject *PrintDialog::DoMakeContent()
  373. {
  374.     Iter next(gPrintManager->printers);
  375.     printernames= new OrdCollection;
  376.     Printer *p;
  377.     
  378.     for (int i= 0; p= (Printer*) next(); i++)
  379.     printernames->Add(new TextItem(i, p->GetName()));
  380.     
  381.     collview= new CollectionView(this, printernames);
  382.     collview->SetMinExtent(Point(cNameMinWidth+20, 0));
  383.     collview->SetId(cIdPrinters);
  384.  
  385.     return
  386.     clu= new VBox(10, (VObjAlign)(eVObjHLeft|eVObjHExpand|eVObjVExpand),
  387.         new Matte(
  388.         new HBox(10, (VObjAlign)(eVObjVTop|eVObjHExpand|eVObjVExpand),
  389.             new VBox(10, (VObjAlign)(eVObjHLeft|eVObjHExpand|eVObjVExpand),
  390.             new Scroller(collview, Point(cNameMinWidth, 16*4), cIdPrinters),
  391.             new BorderItem("Options", options= new ToggleButton(cIdOptions, "Show Page Breaks")),
  392.             new BorderItem("Pages",
  393.                 new HBox(10, eVObjVBase,
  394.                 new TextItem("Print pages from:"),
  395.                 from= new IntField(cIdFrom, 1, 999),
  396.                 new TextItem("to:"),
  397.                 to= new IntField(cIdTo, 1, 999),
  398.                 0
  399.                 )
  400.             ),
  401.             0
  402.             ),
  403.             new VExpander(10,
  404.             new ActionButton(cIdApply,      "Apply Settings", TRUE),
  405.             new ActionButton(cIdPrint,      "Print"),
  406.             new ActionButton(cIdSaveToFile, "Print to file ..."),
  407.             new ActionButton(cIdCancel,     "Cancel"),
  408.             new ActionButton(cHELP,         "Help"),
  409.             0
  410.             ),
  411.             0
  412.         )
  413.         ),
  414.         0
  415.     );
  416. }
  417.  
  418. void PrintDialog::Control(int id, int p, void *v)
  419. {
  420.     switch (id) {
  421.     
  422.     case cIdApply:
  423.     StoreSettings(gPrintManager->currSettings);
  424.     clu->ExtentChanged(clu);
  425.     break;
  426.     
  427.     case cIdPrinters:
  428.     if (p == cPartCollSelect) {
  429.         StoreSettings(gPrintManager->currSettings);
  430.         gPrintManager->InstallPrinter(printernames->At((int)v)->AsString());
  431.         
  432.         Printer *p= gPrintManager->GetPrinter();
  433.         
  434.         if (p && p->GetOptions()) {
  435.         if (clu->Size() > 1)
  436.             clu->SetAt(1, p->GetOptions());
  437.         else
  438.             clu->Add(p->GetOptions());
  439.         p->LoadSettings(gPrintManager->currSettings);
  440.         } else if (clu->Size() > 1)
  441.         clu->Remove(clu->At(1));
  442.         clu->ExtentChanged(clu);
  443.     }
  444.     id= cIdNone;    // don't dismiss
  445.     break;
  446.     
  447.     case cIdSaveToFile:
  448.     if (saveDialog == 0)
  449.         saveDialog= new FileDialog();
  450.     if (saveDialog->ShowInWindow(eFDWrite, GetWindow()) == cIdOk) {
  451.         clu->ExtentChanged(clu);
  452.         StoreSettings(gPrintManager->currSettings);
  453.         gPrintManager->Print(saveDialog->FileName(),
  454.                         from->GetValue(), to->GetValue());
  455.     }
  456.     id= cIdNone;    // don't dismiss
  457.     break;
  458.     
  459.     case cIdPrint:
  460.     clu->ExtentChanged(clu);
  461.     StoreSettings(gPrintManager->currSettings);
  462.     gPrintManager->Print(0, from->GetValue(), to->GetValue());
  463.     id= cIdNone;    // don't dismiss
  464.     break;
  465.     }
  466.     Dialog::Control(id, p, v);
  467. }
  468.  
  469.